<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<meta name="apple-mobile-web-app-status-bar-style" content="black" />
		<title>计划制定</title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<style>
			* {
				/* touch-action: none; */
			}

			.mui-table-title {
				padding: 3px 0;
				font-size: 1.1em;
				width: 100%;
			}

			.mui-text-line {
				width: 82%;
				font-size: .7em;
				padding-left: 1%;
			}

			.mui-table-view-cell {
				padding-right: 50px !important;
			}
			
		</style>
	</head>
	<body>
		<div class="mui-content mui-scroll-wrapper" id="pullrefresh">
			<div class="mui-scroll">
				<div class="mui-card mui-plan-info" id="plans">
					<div class="mui-card-content">
						<ul class="mui-table-view mui-table-view-chevron">
							<li class="mui-table-view-cell" :data-pid="interval.id" v-for="interval in interval_list">
								<a class="mui-navigate-right">
									<div class="mui-table-title mui-ellipsis" v-text="interval.planname" v-cloak><span :class="classObject(interval.isalive)" v-text="interval.isalivename" v-cloak></span></div>
									<span class="mui-badge mui-badge-inverted mui-right" v-text="interval.plantypename" v-cloak></span>
									<p class="mui-text-line mui-text-left mui-ellipsis" v-text="'控制设备:' + interval.controlname" v-cloak></p>
									<p class="mui-text-line mui-text-left mui-ellipsis" v-text="'控制条件:' + interval.contidionname" v-cloak></p>
								</a>
							</li>
							<li class="mui-table-view-cell" :data-pid="condition.id" v-for="condition in condition_list">
								<a class="mui-navigate-right">
									<div class="mui-table-title mui-ellipsis" v-text="condition.planname" v-cloak><span :class="classObject(condition.isalive)" v-text="condition.isalivename" v-cloak></span></div>
									<span class="mui-badge mui-badge-inverted mui-right" v-text="condition.plantypename" v-cloak></span>
									<p class="mui-text-line mui-text-left mui-ellipsis" v-text="'控制设备:' + condition.controlname" v-cloak></p>
									<p class="mui-text-line mui-text-left mui-ellipsis" v-text="'控制条件:' + condition.contidionname" v-cloak></p>
								</a>
							</li>
						</ul>
					</div>
				</div>
			</div>
		</div>
		<script src="js/common.js"></script>
		<script src="js/config.js"></script>
		<script src="../js/func/hui.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/mui.js"></script>
		<script src="js/vue.min.js"></script>
		<script>
			var vm_plan = new Vue({
				el: "#plans",
				data: {
					powerid: "", // 权限编号
					uid: 0, // 用户编号
					interval_list: [], // 时间计划列表
					condition_list: [], // 条件计划列表
				},
				methods: {
					LoadData: function() { // 加载目录结构数据
						// 获取数据结构
						mui.get(setting.GetConfigData(vm_plan.uid),
							function(d) {
								if (d.code == "0") {
									setting.SetCacheConfig(d.result);
								}
							});
					},
					LoadPlanData: function() { // 加载计划配置机构数据
						if (vm_plan.uid == null || vm_plan.powerid == null) return;
						mui.get(setting.GetPlanConfig(vm_plan.uid, vm_plan.powerid),
							function(d) {
								if (d.code == "0" && d.result.data != null) {
									vm_plan.condition_list = [];
									vm_plan.interval_list = [];
									for (var i = 0; i < d.result.data.length; i++) {
										var p = vm_plan.copy(d.result.data[i]);
										var cname = [];
										var ctag = "";
										switch (p.plantype) {
											case "condition":
												ctag = [];
												for (var j = 0; j < p.conditionlist.length; j++) {
													if (!cname.contains(p.conditionlist[j].controlname))
														cname.push(p.conditionlist[j].controlname);
													if (!ctag.contains(p.conditionlist[j].targetname))
														ctag.push(p.conditionlist[j].targetname);
												}
												p.controlname = cname.join(',');
												p.contidionname = ctag.join(',');
												vm_plan.condition_list.push(p);
												vm_plan.condition_list = vm_plan.reverseRankingDate(vm_plan.condition_list, "addtime")
												break;
											case "interval":
												for (var j = 0; j < p.intervallist.length; j++) {
													if (!cname.contains(p.intervallist[j].controlname)) {
														cname.push(p.intervallist[j].controlname);
													}
													ctag = p.intervallist[j].starttime + " ~ " + p.intervallist[j].endtime;
												}
												p.controlname = cname.join(',');
												p.contidionname = ctag;
												vm_plan.interval_list.push(p);
												vm_plan.interval_list = vm_plan.reverseRankingDate(vm_plan.interval_list, "addtime")
												break;
										}
									}
									// 缓存计划列表
									setting.SetPlanDataConfig(d.result.data);
								}
								mui.toast("刷新成功", "div");
								mui('#pullrefresh').pullRefresh().endPulldownToRefresh();
							});
					},
					openUrl: function(url) {
						/* mui.openWindow({
							url: url + vm_plan.powerid
						}); */
						hui.open(url + vm_plan.powerid, {}, true, {});
					},
					opendetail: function(id) {
						hui.open("plan_detail.html?id=" + id, {}, true, {});
						/* mui.openWindow({
							url: "plan_detail.html?id=" + id
						}); */
					},
					copy: function(obj) {
						var newobj = {};
						for (var attr in obj) {
							newobj[attr] = obj[attr];
						}
						return newobj;
					},
					classObject: function(alive) {
						return alive == "active" ? "mui-badge mui-badge-green mui-pull-right" :
							"mui-badge mui-badge-gray mui-pull-right"
					},
					reverseRankingDate(data, p) {
						for (i = 0; i < data.length - 1; i++) {
							for (j = 0; j < data.length - 1 - i; j++) {
								//console.log(Date.parse(data[j][p]));
								if (Date.parse(data[j][p]) < Date.parse(data[j+1][p])) {
									var temp = data[j];
									data[j] = data[j + 1];
									data[j + 1] = temp;
								}
							}
						}
						return data;
					},
				},
				mounted: function() {
					this.$nextTick(function() {
						// 提取用户编号uid,权限编号powerid，获取计划结构信息
						vm_plan.uid =  GetQueryString("uid"); // GetQueryString(uid);
						vm_plan.powerid = GetQueryString("pid"); // '4b749cd28c5d41718510cd5fdcb80aef';
						vm_plan.LoadData();
						// vm_plan.LoadPlanData();
					});
				}
			});
			mui.init({
				pullRefresh: {
					container: '#pullrefresh',
					down: {
						height: 50, //可选,默认50.触发下拉刷新拖动距离,
						auto: true, //可选,默认false.首次加载自动下拉刷新一次
						contentdown: "下拉加载计划信息",
						contentover: "释放立即加载计划信息",
						contentrefresh: "正在加载计划信息...",
						callback: vm_plan.LoadPlanData,
					}
				},
			});

			/** 查看详细信息 */
			mui('.mui-table-view').on('tap', '.mui-table-view-cell', function() {
				var self = this;
				hui.open("./plan_detail.html?id=" + self.dataset.pid, {}, true, {});
				/* mui.openWindow({
					url: "plan_detail.html?id=" + self.dataset.pid
				}); */
			});
		</script>
	</body>
</html>
